package com.example.treehole.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.example.treehole.App;
import com.example.treehole.ChatActivity;

import com.example.treehole.base.BaseBindingFragment;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Chat;
import com.example.treehole.bean.Message;
import com.example.treehole.databinding.FragmentMessageListBinding;
import com.example.treehole.databinding.ItemMsgBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.HashMap;
import java.util.List;


public class MessageListFragment extends BaseBindingFragment<FragmentMessageListBinding> {
    private final BindAdapter<ItemMsgBinding, Chat> adapter = new BindAdapter<ItemMsgBinding, Chat>() {
        @Override
        public ItemMsgBinding createHolder(ViewGroup parent) {
            return ItemMsgBinding.inflate(getLayoutInflater(), parent, false);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bind(ItemMsgBinding item, Chat chat, int position) {

            item.tvName.setText(chat.other.nickname);
            loadImage(chat.other.face, item.ivFace);
            Message message = chat.message;
            if (message != null) {
                item.tvLast.setText(message.content);
                item.tvTime.setText(message.time);
            } else {
                item.tvLast.setText("");
                item.tvTime.setText("");
            }
            if (chat.unread == 0) {
                item.tvUnread.setVisibility(View.GONE);
            } else {
                item.tvUnread.setVisibility(View.VISIBLE);
                item.tvUnread.setText(chat.unread + "");
            }
            item.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(requireContext(), ChatActivity.class).putExtra("chat", chat));
                }
            });
        }
    };

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        viewBinder.rvMsg.setAdapter(adapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResume() {
        super.onResume();
        onHiddenChanged(false);
    }

    private void refresh() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", App.user.id);
        HttpUtil.getList(Api.CHAT_GET, params, Chat.class, new HttpUtil.OnNetListListener<Chat>() {
            @Override
            public void success(List<Chat> data, String message) {
                adapter.getData().clear();
                adapter.getData().addAll(data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void error(String message) {
                toast(message);
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            refresh();
        }
    }
}
